<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use WechatPayV3\WechatPayV3;
class WechatPayComplaint extends Controller
{
    public function mchList()
    {
        $mch[0] = [
            5=>['mch_id'=>'1571615431','data'=>$this->getMchUncomplaintCount('1571615431'),'pay_uniacid'=>5],
            12=>['mch_id'=>'1567597801','data'=>$this->getMchUncomplaintCount('1567597801'),'pay_uniacid'=>12],
            ];
        $mch[1] = [
            17=>['mch_id'=>'1609226509','data'=>$this->getMchUncomplaintCount('1609226509'),'pay_uniacid'=>17],
            24=>['mch_id'=>'1609549160','data'=>$this->getMchUncomplaintCount('1609549160'),'pay_uniacid'=>24],
            ];
        $mch[2] = [
            // 25=>['mch_id'=>'1620905287','data'=>$this->getMchUncomplaintCount('1620905287'),'pay_uniacid'=>25],
            // 33=>['mch_id'=>'1623269128','data'=>$this->getMchUncomplaintCount('1623269128'),'pay_uniacid'=>33],
            77=>['mch_id'=>'1634480723','data'=>$this->getMchUncomplaintCount('1634480723'),'pay_uniacid'=>77],
            147=>['mch_id'=>'1635811722','data'=>$this->getMchUncomplaintCount('1635811722'),'pay_uniacid'=>147],
            242=>['mch_id'=>'1636590491','data'=>$this->getMchUncomplaintCount('1636590491'),'pay_uniacid'=>242],
        ]; 
        $mch[0] =$mch[1]  = [];
        $mch[3] = [
            35=>['mch_id'=>'1630759818','data'=>$this->getMchUncomplaintCount('1630759818'),'pay_uniacid'=>35],
            36=>['mch_id'=>'1632092555','data'=>$this->getMchUncomplaintCount('1632092555'),'pay_uniacid'=>36],
            37=>['mch_id'=>'1634435244','data'=>$this->getMchUncomplaintCount('1634435244'),'pay_uniacid'=>37],
        ];
        //76260AB9E006B759C24F0440F009F038B4E83E45
        $this->assign([
            'mch'=>$mch,
        ]);
        return view();
    }
    
    public function getMchUncomplaintCount($mchId)
    {
        return ['uncomplaint'=> db('datatable_complaints')->where('mch_id',$mchId)->where('complaint_state','<>','已处理完成')->count() ];
    }
    
    public function index()
    {
        $mchId = input('mch_id');
        if(input('page'))
        {
            $this->syncListData($mchId);
            $list = db('datatable_complaints')->where('mch_id',$mchId)->page(input('page'),input('limit'))->orderRaw(' CONVERT( complaint_state USING gbk )  ASC')->order('complaint_time DESC')->select();
            $count = db('datatable_complaints')->where('mch_id',$mchId)->count();
            return ['code'=>0,'count'=>$count,'data'=>$list,'msg'=>''];
        }
        return view();
    }
    
    public function view()
    {
        $complaintId = input('complaint_id');
        $id = input('id');
        //$data = db('datatable_complaints')->where('id',$id)->find();
        $this->syncOneData( input('complaint_id'), input('mch_id'));
        $data = db('datatable_complaints')->where('id',$id)->find();
        $data['complaint_order_info'] = json_decode($data['complaint_order_info'],true);
        $data['service_order_info'] = json_decode($data['service_order_info'],true);
        $data['complaint_media_list'] = json_decode($data['complaint_media_list'],true);
        $data['negotiation_historys'] = json_decode($data['negotiation_historys'],true);
        $this->assign([
            'data'=>$data,
        ]);
        return view();
    }
    
    public function response()
    {
        if(request()->isAjax() )
        {
            $data = input('post.');
            //$data = input();
            $WechatPayV3 = new \WechatPayV3\WechatPayV3();
            $result = $WechatPayV3->responseComplaint($data['complaint_id'],$data['response_content'],$data['mch_id']);
            return $result;
        }
        else
        {
            $id = input('id');
            $this->syncOneData( input('complaint_id'), input('mch_id'));
            $data = db('datatable_complaints')->where('id',$id)->find();
            $data['complaint_order_info'] = json_decode($data['complaint_order_info'],true);
            $data['service_order_info'] = json_decode($data['service_order_info'],true);
            $data['complaint_media_list'] = json_decode($data['complaint_media_list'],true);
            $data['negotiation_historys'] = json_decode($data['negotiation_historys'],true);
            $this->assign([
                'data'=>$data,
            ]);
            return view();
        }
    }
    
    public function complete()
    {
        $mchId = input('mch_id');
        $complaintId = input('complaint_id');
        $WechatPayV3 = new \WechatPayV3\WechatPayV3();
        $result = $WechatPayV3->completeComplaint($complaintId,$mchId);

        return $result;
    }
    
    private function syncListData($mchId)
    {
        //$mchid = input('mchid');
        $WechatPayV3 = new \WechatPayV3\WechatPayV3();
        $result = $WechatPayV3->complaints($mchId);
        $result = json_decode($result,true)['data'];
        $problemType = ['OTHERS'=>'其他类型','REFUND'=>'退款类型的问题投诉','SERVICE_NOT_WORK'=>'服务权益未生效'];
        $complaintState = ['PENDING'=>'待处理','PROCESSING'=>'处理中','PROCESSED'=>'已处理完成'];

        foreach ($result as $k=>$v)
        {
            $v['complaint_order_info'] = json_encode($v['complaint_order_info']);
            $v['service_order_info'] = json_encode($v['service_order_info']);
            $v['complaint_media_list'] = json_encode($v['complaint_media_list']);
            $v['problem_type'] = $problemType[$v['problem_type']];
            $v['complaint_state'] = $complaintState[$v['complaint_state']];
            $v['incoming_user_response'] = $v['incoming_user_response']?'是':'否';
            $v['update_time'] = time();
            $v['mch_id'] = $mchId;
            if(db('datatable_complaints')->where('complaint_id',$v['complaint_id'])->find())
            {
                db('datatable_complaints')->where('complaint_id',$v['complaint_id'])->update($v);
            }
            else
            {
                $v['add_time'] = time();
                db('datatable_complaints')->where('complaint_id',$v['complaint_id'])->insertGetId($v);
            }
        }
        return 1;
    }
    
    private function syncOneData($complaintId,$mchId)
    {
        $WechatPayV3 = new \WechatPayV3\WechatPayV3();
        $result = $WechatPayV3->negotiationHistorys($complaintId,$mchId);
        db('datatable_complaints')->where('complaint_id',$complaintId)->update(['negotiation_historys'=>$result]);
    }
    
}



